//
//  PaiListViewCell.m
//  yigou
//
//  Created by wangdianlei on 2017/2/20.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "PaiListViewCell.h"
#import "OYCountDownManager.h"

@implementation PaiListViewCell

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kCountDownNotification object:nil];
}

- (void)countDownNotification {
    /// 判断是否需要倒计时 -- 可能有的cell不需要倒计时,根据真实需求来进行判断
    if (0) {
        return;
    }
    
    if (self.ldata) {
        NSDateFormatter *date = [[NSDateFormatter alloc] init];
        [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//@"yyyy-MM-dd HH:mm:ss"
        
        NSDate *d = [date dateFromString:self.ldata.end_tm];
        NSTimeInterval late = [d timeIntervalSince1970]*1;
        NSDate *d2 = [date dateFromString:self.ldata.start_tm];
        NSTimeInterval now = [d2 timeIntervalSince1970]*1;
        
        NSTimeInterval cha = late-now;
        
        /// 计算倒计时
        NSInteger countDown = cha - kCountDownManager.timeInterval;
        if (countDown < 0) return;
        /// 重新赋值
        if (countDown == 0) {
            self.lblock.timeline.text = @"拍卖结束";
        } else if (countDown > 86400) {
            self.lblock.timeline.text = [NSString stringWithFormat:@"距结束%02zd:%02zd:%02zd:%02zd", (countDown/3600)/24, (countDown/3600)%24, (countDown/60)%60, countDown%60];
            //block.timeline.font = [FontTool fontWithSize:10];
        } else {
            self.lblock.timeline.text = [NSString stringWithFormat:@"距结束%02zd:%02zd:%02zd", countDown/3600, (countDown/60)%60, countDown%60];
            //block.timeline.font = [FontTool fontWithSize:12];
        }
    }
    if (self.rdata) {
        NSDateFormatter *date = [[NSDateFormatter alloc] init];
        [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//@"yyyy-MM-dd HH:mm:ss"
        
        NSDate *d = [date dateFromString:self.rdata.end_tm];
        NSTimeInterval late = [d timeIntervalSince1970]*1;
        NSDate *d2 = [date dateFromString:self.rdata.start_tm];
        NSTimeInterval now = [d2 timeIntervalSince1970]*1;
        
        NSTimeInterval cha = late-now;
        
        /// 计算倒计时
        NSInteger countDown = cha - kCountDownManager.timeInterval;
        if (countDown < 0) return;
        /// 重新赋值
        if (countDown == 0) {
            self.rblock.timeline.text = @"拍卖结束";
        } else if (countDown > 86400) {
            self.rblock.timeline.text = [NSString stringWithFormat:@"距结束%02zd:%02zd:%02zd:%02zd", (countDown/3600)/24, (countDown/3600)%24, (countDown/60)%60, countDown%60];
            //block.timeline.font = [FontTool fontWithSize:10];
        } else {
            self.rblock.timeline.text = [NSString stringWithFormat:@"距结束%02zd:%02zd:%02zd", countDown/3600, (countDown/60)%60, countDown%60];
            //block.timeline.font = [FontTool fontWithSize:12];
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    CGFloat thewidth = (ScreenWidth - 30 - 5) / 2;
    self.lblock = [PaiListViewBlock new];
    self.lblock.frame = CGRectMake(15, 0, thewidth, thewidth);
    [self.contentView addSubview:self.lblock];
    
    self.rblock = [PaiListViewBlock new];
    self.rblock.frame = CGRectMake(15 + 5 + thewidth, 0, thewidth, thewidth);
    [self.contentView addSubview:self.rblock];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification)    name:kCountDownNotification object:nil];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)fillWithData:(MMMarketPai *)ldata right:(MMMarketPai *)rdata {
    self.ldata = ldata;
    self.rdata = rdata;
    if (ldata == nil) {
        self.lblock.hidden = YES;
    } else {
        self.lblock.hidden = NO;
        NSString *imgurl = @"";
        imgurl = ldata.goods_pic_url;
        NSArray *array = [ldata.goods_pic_url componentsSeparatedByString:@","];
        if (array.count > 0) {
            imgurl = array[0];
        }
        [self.lblock.img sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        //block.img.image = [UIImage imageNamed:@"placeholder.png"];
        
        NSString *changeStr = @"当前价: ";
        changeStr = [changeStr stringByAppendingString:@"¥"];
        changeStr = [changeStr stringByAppendingString:ldata.last_auction_price];
        //NSString *changeStr = one.last_auction_price;
        NSString *subStr = @"当前价: ";
        NSRange range = [changeStr rangeOfString:subStr];
        NSMutableAttributedString *strAttribute = [[NSMutableAttributedString alloc] initWithString:changeStr];
        [strAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:range];
        [strAttribute addAttribute:NSFontAttributeName value:[FontTool fontWithSize:11] range:range];
        self.lblock.price.attributedText = strAttribute;
        
        NSDateFormatter *date=[[NSDateFormatter alloc] init];
        [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//@"yyyy-MM-dd HH:mm:ss"
        NSDate *d = [date dateFromString:ldata.end_tm];
        
        NSTimeInterval late = [d timeIntervalSince1970]*1;
        
        NSDate *d2 = [date dateFromString:ldata.start_tm];
        NSTimeInterval now = [d2 timeIntervalSince1970]*1;
    
        self.lblock.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLeft:)];
        tap.numberOfTapsRequired = 1;
        [self.lblock addGestureRecognizer:tap];
    }
    
    if (rdata == nil) {
        self.rblock.hidden = YES;
    } else {
        self.rblock.hidden = NO;
        NSString *imgurl = @"";
        imgurl = rdata.goods_pic_url;
        NSArray *array = [rdata.goods_pic_url componentsSeparatedByString:@","];
        if (array.count > 0) {
            imgurl = array[0];
        }
        [self.rblock.img sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        
        NSString *changeStr = @"当前价: ";
        changeStr = [changeStr stringByAppendingString:@"¥"];
        changeStr = [changeStr stringByAppendingString:rdata.last_auction_price];
        //NSString *changeStr = one.last_auction_price;
        NSString *subStr = @"当前价: ";
        NSRange range = [changeStr rangeOfString:subStr];
        NSMutableAttributedString *strAttribute = [[NSMutableAttributedString alloc] initWithString:changeStr];
        [strAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:range];
        [strAttribute addAttribute:NSFontAttributeName value:[FontTool fontWithSize:11] range:range];
        self.rblock.price.attributedText = strAttribute;
        
        NSDateFormatter *date=[[NSDateFormatter alloc] init];
        [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//@"yyyy-MM-dd HH:mm:ss"
        NSDate *d = [date dateFromString:rdata.end_tm];
        
        NSTimeInterval late = [d timeIntervalSince1970]*1;
        
        NSDate *d2 = [date dateFromString:rdata.start_tm];
        NSTimeInterval now = [d2 timeIntervalSince1970]*1;
        
        self.rblock.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRight:)];
        tap.numberOfTapsRequired = 1;
        [self.rblock addGestureRecognizer:tap];
    }
}

- (void)clickLeft:(UITapGestureRecognizer*)recognizer {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickPaiList:)]) {
        [self.delegate clickPaiList:self.ldata];
    }
}

- (void)clickRight:(UITapGestureRecognizer*)recognizer {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickPaiList:)]) {
        [self.delegate clickPaiList:self.rdata];
    }
}

@end
